package com.scalar.chiptrack.sales.reports.actions;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.ExcelUtils;
import com.scalar.chiptrack.operations.process.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.operations.process.FinishedGoods;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.sales.reports.forms.QuotationReportForm;
import com.scalar.chiptrack.sales.reports.dao.DataDumpDao;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.commons.Constants;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * User: Sujan
 * Date: Aug 26, 2008
 * Time: 4:17:17 PM
 */
public class QuotationReportAction extends ChipTrackExtendedAction
{

    public ActionForward executeRequest( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {

        String action = request.getParameter( "action" );
        if ( action == null )
        {
            action = "quotationReportSearch";
        }
        QuotationReportForm quotationReportForm = (QuotationReportForm) form;
        if ( "quotationReportSearch".equalsIgnoreCase( action ) )
        {
            request.setAttribute ( "productList", loadManufacturingProducts() );
            request.setAttribute ( "customerList", CustomerDAO.getAllCustomerNamesByUserTypeByCustomerType ( null, pUser, 1 ) );
            request.setAttribute ( "endCustomerList", NewCustomerDAO.getAllCustomerNamesByCustomerType(Constants.CUSTOMER_TYPE_END_CUSTOMER) );
            request.setAttribute ( "endUserList", NewCustomerDAO.getAllCustomerNamesByCustomerType(Constants.CUSTOMER_TYPE_END_USER) );
            request.setAttribute ( "regionList",  loadRegions() );
            request.setAttribute ( "salesPersonList", NewCustomerDAO.loadAllSalesPersonsWithRegions() );

            return mapping.findForward( "search" );
        }
        else if ( "quotationReportxls".equalsIgnoreCase( action ) )
        {
            ArrayList quotationDetailsList = DataDumpDao.getQuotationReport( null, quotationReportForm.getCustomerId(), quotationReportForm.getProductId(), quotationReportForm.getRegionName(),
                                                            quotationReportForm.getEndUserId(), quotationReportForm.getEndCustomerId(), quotationReportForm.getSalesPersonName(), quotationReportForm.getFromDate(), quotationReportForm.getToDate() );

            Workbook workbook = DataDumpReportsHelper.getWorkbook( quotationDetailsList, "Quotation Report",quotationReportForm.getFromDate(),quotationReportForm.getToDate(),null );
            //Nov_4_2003_08_14_11
            String fileName = "QuotationReport_" + DateUtil.formatDate( new Date(), "MMM_dd_yyyy_HH_mm_ss" );

            ExcelUtils.generateExcelReport( workbook, fileName, response );
        }

        return null;
    }

    private ArrayList loadManufacturingProducts( ) throws Exception
    {
        ArrayList parts = new ArrayList ();
        Iterator list   = null;
        list = FinishedGoodsDAO.loadAllFinishedGoods().iterator ();
        while ( list.hasNext () )
        {
            FinishedGoods product = ( FinishedGoods ) list.next ();
            parts.add ( new org.apache.struts.util.LabelValueBean ( product.getManufacturingPartNo (), product.getProductId () + "" ) );
        }
        return parts;
    }
    private ArrayList loadRegions( ) throws Exception
    {
        ArrayList parts = new ArrayList ();
        Iterator list   = null;
        list = CustomerDAO.getAllRegionNames().iterator ();
        while ( list.hasNext () )
        {
            String regionName = ( String ) list.next ();
            parts.add ( new org.apache.struts.util.LabelValueBean ( regionName, regionName ) );
        }
        return parts;
    }
}
